package com.zero.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zero.yygh.cmn.client.DictFeignClient;
import com.zero.yygh.enums.DictEnum;
import com.zero.yygh.model.user.Patient;
import com.zero.yygh.user.mapper.PatientMapper;
import com.zero.yygh.user.service.PatientService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zero
 * @desc
 * @create 2021/4/12
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Resource
    private DictFeignClient dictFeignClient;

    /**
     * 根据用户id查询全部就诊人信息
     * @param userId 用户id
     * @return java.util.List<com.zero.yygh.model.user.Patient>
     * @author zero
     * @date 2021/4/12 20:20
     */
    @Override
    public List<Patient> findAllUserId (Long userId) {
        //根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        //通过远程调用，得到编码对应具体内容，查询数据字典表内容
        //其他参数封装
        patientList.forEach(this::packPatient);
        return patientList;

    }

    /**
     * 根据就诊人Id查询就诊信息
     * @param id id
     * @return com.zero.yygh.model.user.Patient
     * @author zero
     * @date 2021/4/12 20:20
     */
    @Override
    public Patient getPatientId (Long id) {
        return this.packPatient(baseMapper.selectById(id));
    }

    /**
     * 封装就诊人信息
     * @author zero
     * @date 2021/4/12 20:26
     * @param patient 就诊人信息
     * @return com.zero.yygh.model.user.Patient
     */
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指联系人证件
        String certificatesTypeString =
                dictFeignClient.getParentName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeString;
        if(StringUtils.isEmpty(patient.getContactsCertificatesType())) {
            contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode());
        }else {
            contactsCertificatesTypeString = dictFeignClient.getParentName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        }
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }

}
